package com.server.server;

import org.apache.mina.core.session.IoSession;

import com.server.app.PlayerManager;
import com.server.object.Player;
import com.server.protocols.SProtocols;
import com.server.request.SeBroadcastReq;
import com.server.response.SeBroadcastResp;
import com.server.status.SocketState;
import com.webserver.server.net.BaseMessage;
import com.webserver.server.net.BaseServer;

public class BroadcastServer implements BaseServer {
	public int GetProtocol() {
		return SProtocols.SEBROADCAST;
	}

	public BaseMessage GetRequest() {
		return new SeBroadcastReq();
	}

	public BaseMessage handle(IoSession is,BaseMessage request) {
		Player player = PlayerManager.GetInstance().GetPlayer(request.getUserInfo().uid);
		SeBroadcastResp resp;
		if(player != null) {
			resp = new SeBroadcastResp(SocketState.SUCCESSED.ordinal(), ((SeBroadcastReq)request).key);
			if(player.room != null) {
				player.room.Broadcast(player.uid, ((SeBroadcastReq)request).data, ((SeBroadcastReq)request).key, ((SeBroadcastReq)request).isReturn == 1);
			}else {
				System.out.println("room 是null的");
				resp = new SeBroadcastResp(SocketState.NONEXISTENT.ordinal(),((SeBroadcastReq)request).key);
			}
		}else {
			System.out.println("player 是null的");
			resp = new SeBroadcastResp(SocketState.RELOGIN.ordinal(),((SeBroadcastReq)request).key);
		}
		return resp;
	}
}
